package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.common.exception.LeaseException;
import com.atguigu.lease.common.result.ResultCodeEnum;
import com.atguigu.lease.model.entity.SystemPost;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.model.enums.BaseStatus;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.SystemPostService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.service.UserInfoService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author liubo
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {

    @Resource
    private SystemUserMapper systemUserMapper;
    @Autowired
    private SystemPostService systemPostService;
    @Autowired
    private UserInfoService userInfoService;

    @Override
    public IPage<SystemUserItemVo> findSystemUserItemByPage(IPage<SystemUserItemVo> page, SystemUserQueryVo queryVo) {
        return systemUserMapper.findSystemUserItemByPage(page, queryVo);
    }

    @Override
    public SystemUserItemVo findSystemUserItemVoById(Long id) {
        SystemUser systemUser = this.getById(id);
        Long userPostId = systemUser.getPostId();
        systemUser.setPassword(null);
        if (userPostId == null) {
            throw new LeaseException(ResultCodeEnum.FAIL);
        }
        SystemPost systemPost = systemPostService.getById(userPostId);
        if (systemPost == null) {
            throw new LeaseException(ResultCodeEnum.FAIL);
        }
        SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
        BeanUtils.copyProperties(systemUser, systemUserItemVo);
        systemUserItemVo.setPostName(systemPost.getName());

        return systemUserItemVo;
    }

    @Override
    public boolean isUserNameAvailable(String username) {
        LambdaQueryWrapper<SystemUser> queryWrapper = new LambdaQueryWrapper<SystemUser>()
                .eq(SystemUser::getUsername, username);
        long count = count(queryWrapper);
        return count == 0;
    }

    //todo system_user表的数据是否与user_info里的数据有关联
    @Override
    public void removeSystemUseById(Long id) {
        removeById(id);
    }

    @Override
    public void updateStatusById(Long id, BaseStatus status) {
        LambdaUpdateWrapper<SystemUser> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SystemUser::getId, id).set(SystemUser::getStatus, status);
        update(updateWrapper);
    }

}


